//判断浏览区是否支持canvas
function isSupportCanvas(){
    var elem = document.createElement('canvas');
	return !!(elem.getContext && elem.getContext('2d'));
}
/*
 * 获得这个党支部的信息
 * 中心点
 * 活动
 * 志愿者
 * 党支部
 * 
 */
function getThisPartyBranchInfo(){
	var obj = {};
	$.ajax({
		'url':'data/partyBranch.json',
		'async':false,
		"success": function(data){
        	obj = data;
      	}
	})
	return obj;
}

//创建标注
function addMarker(point,t,i,id){
	var marker = new BMap.Marker(point,{icon:iconArr[i]});
	marker.id = id;
	map.addOverlay(marker);
	marker.setTitle(t);
	marker.addEventListener("click",function(e){
		 showInfoWindow(this,id);
	});
}

//
function showInfoWindow(e,id){
	var type = "";
	var type_title = e.getTitle();
	var iconSpan = "";
	console.log(type_title);
	if(type_title == "党支部"){
		type = 0;
		iconSpan = '<span class="i_icon type_icon_1"></span>';
	}else if(type_title == "活动"){
		type = 1;
		iconSpan = '<span class="i_icon type_icon_2"></span>';
	}else if(type_title == "志愿者"){
		type = 2;
		iconSpan = '<span class="i_icon type_icon_3"></span>';
	}else{
		type = 0;
		iconSpan = '<span class="i_icon type_icon_1"></span>';
	}
	if(type == 0){
		cxApi.ajax({
		    url: "http://1p88698g42.iask.in:8085/queryPartyDetail",
		    data: {
		        deptId:id,
		        request_userid : "admin",
		        request_time : Date.parse(new Date())/1000
		    },
		    success: function (data) {
		    	$("#aContent_"+type +" .aContent_title").html(iconSpan + data.deptName);
		    	$("#aContent_"+type +" .deptAddress").html(data.deptAddress);
		    	var activities = data.activities;
		    	var str = "";
		    	for (var i = 0; i < activities.length; i++) {
		    		
		    		var unixTimestamp = new Date( activities[i].time*1000 ) ;
					commonTime = unixTimestamp.toLocaleString();
					console.log(commonTime);
					str = "<tr>"+
								"<th scope='row'>1</th> <td>"+commonTime+"</td>"+
								"<td><a onclick='showActivityModal(\""+activities[i].activityId+"\",\"1\")'>"+activities[i].title+"</a></td>"+
								"<td></td>"+
							"</tr> ";
		    	}
		    	if(activities.length == 0){
		    		str = "<tr><td colspan='3' style='text-align: center;'>最近没有开展的活动</td></tr> ";
		    	}
		    	$("#aContent_"+type +" .table").html(str);
		    	var aContent =$("#aContent_"+type).html();
		    	var infoWindow = new BMap.InfoWindow(aContent);
				infoWindow.setWidth(500)
				e.openInfoWindow(infoWindow);
		    }
		});
	}else{
		cxApi.ajax({
		    url: "http://1p88698g42.iask.in:8085/queryActivityDetail",
		    data: {
		        activityId:id,
		        request_userid : "admin",
		        request_time : Date.parse(new Date())/1000
		    },
		    success: function (data) {
		    	console.log(type);
		    	//活动
		    	if (type == "1") {
		    		$("#aContent_"+type +" .aContent_title").html(iconSpan + data.activityMap.title);
		    		$("#aContent_"+type +" .deptName").html( data.activityMap.deptName);
		    		$("#aContent_"+type +" .panel-content").html( data.activityMap.content);
		    		$("#aContent_"+type +" .detaliClick").html('<a onclick="showActivityModal(\''+id+'\',\'1\')">查看详情</a>');
		    		
		    	} else{
		    		$("#aContent_"+type +" .aContent_title").html(iconSpan +  data.activityMap.project_name);
		    		$("#aContent_"+type +" .deptName").html( data.activityMap.deptName);
		    		$("#aContent_"+type +" .panel-content").html( data.activityMap.content);
		    		$("#aContent_"+type +" .detaliClick").html('<a onclick="showActivityModal(\''+id+'\',\'2\')">查看详情</a>');
		    	}
		    	
		    	
		    	
		    	var aContent =$("#aContent_"+type).html();
		    	var infoWindow = new BMap.InfoWindow(aContent);
				infoWindow.setWidth(500);
				e.openInfoWindow(infoWindow);
		    }
		});
	}
	
	
	 
	 
}
//隐藏标注
function showAHideMarker(type,obj){
	var allOverlay = map.getOverlays();
	console.log();
	if($(obj).children(".badge").hasClass("badge-top-active")){
		$(obj).children(".badge").removeClass("badge-top-active");
	}else{
		$(obj).children(".badge").addClass("badge-top-active");
	}
	for (var i = 0; i < allOverlay.length ; i++){
		console.log(allOverlay[i].getTitle());
		if(allOverlay[i].getTitle() == type){
			if(allOverlay[i].isVisible()){
				allOverlay[i].hide();
			}else{
				allOverlay[i].show();
			}
		}
		
	}
}
//展示详情页面
function showActivityModal(id,type){
	console.log(type);
	cxApi.ajax({
		    url: "http://1p88698g42.iask.in:8085/queryActivityDetail",
		    data: {
		        activityId:id,
		        request_userid : "admin",
		        request_time : Date.parse(new Date())/1000
		    },
		    success: function (data) {
		    	var activityMap = data.activityMap;
		    	console.log(data);
		    	if(type == 1){
		    		$("#activityModal .modal-title").html(activityMap.title)
		    	}else{
		    		$("#activityModal .modal-title").html(activityMap.project_name)
		    	}
		    	
		    	
		    	
		    	
		    	
		    	
		    	$('#activityModal').modal("show");
		    	
		    	
		    	
		    }
		});
	
}


/*
 * 查询组织架构
 */
function queryCompanyDept(){
	var company_id = 17;
	var company_name = "根节点";
	$(".c_left_box .Contacts").show();
	$(".c_left_box .roleList").hide();
	$('.angular-ui-tree-node').empty(); 
	var html="<div style=\"position:relative;width:100%;\" class=\"area\">"
		+"<a class='dep-name dep-name-hl' href=\"javascript:void(0)\" title=\""+company_name+"\" onclick=\"querySubChildren('"+company_id+"','"+company_name+"','"+company_id+"','"+company_name+"',this,'root');\"> "
		+"	<i class=\"iconfont icon-dept\" style=\"font-size:18px;\">&#xe61b</i> "								
		+"	<span>"+company_name+"</span>"
		+"</a> "
		+"<a class=\"dep-tree-pos-btn close\" href=\"javascript:void(0)\"  onclick=\"querySubDept('"+company_id+"','"+company_name+"',this);\"> "								
		+"	<span class=\"iconfont icon-collapsed\" style=\"color: #fff;\">&#xe7c0;</span>"
		+"</a> "
		+"</div>";
	$(".angular-ui-tree-node").append(html);
	$(".angular-ui-tree-node").find(".dep-name")[0].click();	
	setTimeout("$('.angular-ui-tree-node').find('.dep-tree-pos-btn')[0].click()", 500);
	$(".c_right_box .Contacts").show();
	$(".c_right_box .role").hide();
}



/**
 * 查询子部门列表
 * @param dept_id
 * @param dept_name
 * @param obj
 */
function querySubDept(dept_id,dept_name,obj){
	var perm_id = "B0002M";
	dept_name=dept_name.replace(/\r\n/g,"");
	dept_name=dept_name.replace(/\n/g,"");
	if(obj==null || $(obj).attr("class").indexOf("close")>0){
		$(obj).addClass("open");
		$(obj).removeClass("close");
		$(obj).find("span").html("&#xe7c0;");
		cxApi.ajax({
	        url: "http://1p88698g42.iask.in:8085/queryDeptMembers",
	        data: {
	            dept_id:dept_id,
	            request_userid : "admin",
	            request_time : Date.parse(new Date())/1000
	        },
	        success: function (data) {
	           if(data.result=="success"){	
					$(obj).parent().parent().find("ul").remove();
					var sub_depts=data.dept_list;
					if(sub_depts.length>0){					
						var $ul= $("<ul style='margin-left:15px;'></ul>");				
						for(var i = 0; i < sub_depts.length; i++){	
							var name=sub_depts[i].dept_name;
							name = name.replace(/\r\n/g,"");
							name = name.replace(/\n/g,""); 
							var deptId=sub_depts[i].dept_id;	

							var li="<li class='angular-ui-tree-node'>"
								+"<div style=\"position:relative;width:100%;\" class=\"area\">"
								+"<a class=\"dep-name\" href=\"javascript:void(0)\" title=\""+name+"\" onclick=\"querySubChildren('"+deptId+"','"+name+"','"+dept_id+"','"+dept_name+"',this);\"> "
								+"	<i class=\"iconfont icon-dept\" style=\"font-size:15px;\">&#xe6ab</i> "								
								+"	<span>"+name+"</span>"
								+"	<font>&nbsp;（"+sub_depts[i].dept_members+"人）</font>"
								+"</a> "
								+"<a class=\"dep-tree-pos-btn close\" href=\"javascript:void(0)\"  onclick=\"querySubDept('"+deptId+"','"+name+"',this);\"> "								
								+"	<span class=\"iconfont icon-collapsed\" style=\"color: #c5c5c5;\">&#xe7bf;</span>"
								+"</a> "
								+"</div>"
								+"</li>";
							$($ul).append(li);
						}					
						$(obj).parent().parent().append($ul);					
					}
				}	
	        },
	        error: function(data){
	        	console.log(data);
	        }
	    });
		
	}else{
		$(obj).addClass("close");
		$(obj).removeClass("open");
		$(obj).find("span").html("&#xe7bf;");
		$(obj).parent().parent().find("ul").remove();
	}
}

//查询 所有点 并且绘制
function querySubChildren(dept_id,dept_name,parendId,parendName,obj,isParent){
	console.log(dept_id);
	cxApi.ajax({
	    url: "http://1p88698g42.iask.in:8085/queryDeptAllPoint",
	    data: {
	        deptId:dept_id,
	        request_userid : "admin",
	        request_time : Date.parse(new Date())/1000
	    },
	    success: function (data) {
	        
			var branchNode = data;
			console.log(branchNode);
			var allOverlay = map.getOverlays();
			map.clearOverlays(); 
			if(obj!=null){
				/*
				 * 判断是否多次点击自身
				 * if($(obj).attr("class").indexOf("dep-name-hl")>0){
					if(dept_name==oldDept){
						return;
					}			
				}*/
				$(".angular-ui-tree-node .dep-name").removeClass("dep-name-hl");
				$(".angular-ui-tree-node .icon-collapsed").css("color","#c5c5c5");
				
				$(obj).parent().find(".icon-collapsed").css("color","#fff");
				$(obj).addClass("dep-name-hl");	
			}
			if(branchNode.result == "success"){
				var pt = new BMap.Point(branchNode.centerNode.lng, branchNode.centerNode.lat);
				addMarker(pt,"党支部",0,branchNode.centerNode.id);
				map.centerAndZoom(pt, 15);
				//党支部
				var partyBranch = branchNode.partyBranch;
				for (var i = 0; i < partyBranch.length; i++) {
					let pt = new BMap.Point(partyBranch[i].lng, partyBranch[i].lat);
					addMarker(pt,"党支部",0,partyBranch[i].id);
				}
				$(".legend-map .badge").eq(0).html(partyBranch.length+1);
				
				//志愿者
				var volunteer = branchNode.volunteer;
				for (var i = 0; i < volunteer.length; i++) {
					let pt = new BMap.Point(volunteer[i].lng, volunteer[i].lat);
					addMarker(pt,"志愿者",2,volunteer[i].id);
				}
				$(".legend-map .badge").eq(2).html(volunteer.length);
				
				
				//活动
				var activity = branchNode.activity;
				for (var i = 0; i < activity.length; i++) {
					let pt = new BMap.Point(activity[i].lng, activity[i].lat);
					addMarker(pt,"活动",1,activity[i].id);
				}
				
				$(".legend-map .badge").eq(1).html(activity.length);
				
				
			}
	    }
	});
}


function iniTop5(){
	cxApi.ajax({
	        url: "http://1p88698g42.iask.in:8085/queryActivityTOP",
	        data: {
	            pageSize :5,
	            deptId:17,
	            deptSize:5,
	            request_userid : "admin",
	            request_time : Date.parse(new Date())/1000
	        },
	        success: function (data) {
	        	console.log(data);
	        }
	    });
}
